package com.ittest.mapper.system;

import com.ittest.domain.system.Permission;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface PermissionMapper {

    @Select("select distinct" +
            " p.id, p.parentId, p.permissionName, p.url , p.label " +
            " from " +
            " permission p, " +
            " role_permission rp, " +
            " users_role ur " +
            " where  " +
            " p.`id` = rp.`permissionId` and " +
            " rp.`roleId` = ur.roleId  and " +
            " ur.userId =#{id}")
    List<Permission> findPermissionByUserId(String id);

    @Select("select * from permission")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "parentId",property = "parentId"),
            @Result(column = "permissionName",property = "permissionName"),
            @Result(column = "url",property = "url"),
            @Result(column = "label",property = "label")
    })
    List<Permission> findAll();


    @Insert("insert into permission values (#{id},#{parentId},#{permissionName},#{url},#{label})")
    void save(Permission permission);

    @Delete("delete from permission where id=#{id}")
    void delete(String id);

    void deleteById(String[] id);

    @Select("select id,parentId,permissionName,url,label," +
            " case" +
            " when id in (select permissionId from role_permission where roleId = #{id}) " +
            " then '1' " +
            " else '0' " +
            " end " +
            " as flag " +
            " from " +
            " permission")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "parentId",property = "parentId"),
            @Result(column = "permissionName",property = "permissionName"),
            @Result(column = "url",property = "url"),
            @Result(column = "label",property = "label"),
            @Result(column = "flag",property = "flag")
    })
    List<Permission> findPermissionByRole(String id);
}
